import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/cons/global_cons.dart';
import 'package:flutter_shop/pages/common/common_input_page/entity/common_input_data.dart';
import 'package:flutter_shop/pages/common/common_router.dart';
import 'package:flutter_shop/pages/common/login/entity/base_info.dart';
import 'package:flutter_shop/utils/log_utils.dart';
import 'package:flutter_shop/utils/utils.dart';

import 'routers.dart';

///Title:fluro的路由跳转工具类 <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-03-05 <br/>
///version 1.0 <br/>
class NavigatorUtils {
  ///跳转页面
  static void push(BuildContext context, String path, {Map<String, dynamic> params, bool replace = false, bool clearStack = false}) {
    FocusScope.of(context).unfocus();
    Routers.router.navigateTo(context, _convert2OkPath(path, params), replace: replace, clearStack: clearStack, transition: TransitionType.native);
  }

  ///跳转页面接收结果
  static void pushResult<T>(BuildContext context, String path, Function(T) function, {Map<String, dynamic> params, bool replace = false, bool clearStack = false}) {
    FocusScope.of(context).unfocus();
    Routers.router.navigateTo(context, _convert2OkPath(path, params), replace: replace, clearStack: clearStack, transition: TransitionType.native).then((result) {
      // 页面返回result为null
      if (result == null) {
        return;
      }
      function(result);
    }).catchError((error) {
      print('$error');
    });
  }

  ///跳转页面检查登录
  static void pushCheckLogin(BuildContext context, String path, {Map<String, dynamic> params}) {
    if (!BaseInfo.isLogin()) {
      pushResult(context, CommonRouter.loginPage, (result) {
        if (result == GlobalCons.RESULT_OK) {
          push(context, path, params: params);
        }
      });
    } else {
      push(context, path, params: params);
    }
  }

  /// 返回
  static void goBack(BuildContext context) {
    FocusScope.of(context).unfocus();
    Navigator.maybePop(context);
  }

  /// 带参数返回
  static void goBackWithParams(BuildContext context, result) {
    FocusScope.of(context).unfocus();
    Navigator.maybePop(context, result);
  }

  /// 跳到WebView页
  static void goWebViewPage(BuildContext context, String title, String url) {
    push(context, CommonRouter.webViewPage, params: {GlobalCons.KEY_TITLE: title, GlobalCons.KEY_URL: url});
  }

  /// 跳转到常规输入页
  static void goCommonInputPage(
    BuildContext context, {
    String title = "请输入",
    String content = "",
    String hintText = "请输入",
    int maxLength = 200,
    String inputType,
    Function callback,
  }) {
    CommonInputData entity = CommonInputData(
      title: title,
      content: content,
      hintText: hintText,
      maxLength: maxLength,
      inputType: inputType,
    );
    pushResult(context, CommonRouter.inputPage, callback, params: {GlobalCons.KEY_DATA: Utils.parseObj2Json(entity)});
  }

  ///将path和传递的参数拼接以及处理
  static String _convert2OkPath(String path, Map<String, dynamic> params) {
    String query = "";
    if (params != null) {
      int index = 0;

      StringBuffer sb = new StringBuffer();
      params.forEach((k, v) {
        String value;
        if (v != null) {
          value = Uri.encodeComponent(v);
        }
        if (index == 0) {
          sb.write("?");
        } else {
          sb.write("&");
        }

        sb.write("$k=$value");

        index++;
      });

      query = sb.toString();
    }
    Log.d(path + query, tag: "NavigatorUtils");

    return path + query;
  }
}
